Stack = {top_ = 0}

function Stack:new (object)
	object = object or {}

	self.__index = self
	setmetatable(object, self)
	object.top_ = #object

	return object
end

function Stack:push (element)
	self.top_ = self.top_ + 1
	self[self.top_] = element
end

function Stack:pop ()
	if self.top_ == 0 then
		return nil
	end

	local tmp = self[self.top_]
	self[self.top_] = nil
	self.top_ = self.top_ - 1

	return tmp
end

function Stack:top ()
	return self[self.top_] or nil
end

function Stack:isempty ()
	if self.top_ == 0 then
		return true
	end
	return false
end

s = Stack:new{1, 3}
print("Stack: ")
print(s:pop())
s:push(4)
print(s:top())
print(s:isempty())

-- inheritance
StackQueue = Stack:new{}

function StackQueue:insertBottom (element)
	for i = self.top_, 1, -1 do
		self[i + 1] = self[i]
	end
	self.top_ = self.top_ + 1
	self[1] = element
end

print("StackQueue: ")
sq = StackQueue:new{1, 2}
print(sq:pop()) 		-- 2
sq:push(4)
sq:insertBottom(100)
print(sq:pop()) 		-- 4
print(sq:pop()) 		-- 1
print(sq:pop()) 		-- 100
print(s:isempty()) 	-- false

